package cn.fl.mcr.service.impl;

import java.util.List;

import cmm.user.facade.system.api.ISysUsrQueryApiService;
import cmm.user.facade.system.condition.SysUsrSysRQueryCondition;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.cache.framework.utils.PropertiesCacheUtils;
import cn.fl.mcr.service.IMcrCityOpenService;
import cn.fl.mcr.transform.McrCityOpenVOTransform;
import cn.fl.mcr.vo.McrCityOpenVO;
import cn.fl.system.transform.SysUsrSysRVOTransform;
import cn.fl.system.vo.SysUsrSysRVO;
import cn.lionbridgecapital.loan.model.base.Pager;
import cn.lionbridgecapital.loan.model.dto.McrCityOpenDTO;
import cn.lionbridgecapital.loan.service.api.IMcrCityOpenApiService;
import fl.constant.facade.framework.constant.SystemConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.fl.framework.base.context.CurrentThreadContext;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.exception.BaseException;

/**
 * @Description:  客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class McrCityOpenServiceImpl implements IMcrCityOpenService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(IMcrCityOpenService.class);
	
	/**
	 * TODO 自定义 API 暴露服务
	 */
	@Autowired
	private IMcrCityOpenApiService mcrCityOpenApiService;

	@Autowired
	private ISysUsrQueryApiService sysUsrQueryApiService;

	@Override
	public McrCityOpenVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//TODO 业务逻辑
		McrCityOpenDTO dto = mcrCityOpenApiService.selectMcrCityOpenDTOById(id);
		return McrCityOpenVOTransform.toVO(dto);
	}

	@Override
	public Long add(McrCityOpenVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		return this.mcrCityOpenApiService.addCityOpen(McrCityOpenVOTransform.toDTO(entity));

	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		return this.mcrCityOpenApiService.removeById(id);
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
    		BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(McrCityOpenVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		return this.mcrCityOpenApiService.modifyMcrCityOpenDTO(McrCityOpenVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		page = this.mcrCityOpenApiService.searchMcrCityOpenListPage(page);
		if(null != page && CollectionUtils.isNotEmpty(page.getResultList())){
			List<McrCityOpenVO> voList=McrCityOpenVOTransform.toVOList((List<McrCityOpenDTO>)page.getResultList());
			for (McrCityOpenVO vo : voList) {
				if(vo.getOrgType().equals("zk")){
					vo.setOrgTypeName("重卡");
				}else if(vo.getOrgType().equals("yc")){
					vo.setOrgTypeName("优车");
				}else if(vo.getOrgType().equals("kc")){
					vo.setOrgTypeName("客车");
				}else if(vo.getOrgType().equals("xny")){
					vo.setOrgTypeName("新能源");
				}else if(vo.getOrgType().equals("cdd")){
					vo.setOrgTypeName("车抵贷");
				}
				//获取用户信息
				SysUsrSysRQueryCondition condition = new SysUsrSysRQueryCondition();
				condition.setUsrId(vo.getMdfUsrId());
				condition.setSysCd(PropertiesCacheUtils.getProperty(SystemConstant.FL_SERVICE_CD));
				condition.setIsDel(BaseConstant.IS_YESNO_NO);
				List<SysUsrSysRVO> usrList = SysUsrSysRVOTransform.toVOList(sysUsrQueryApiService.searchSysUsrSysRList(condition));
				if(usrList != null && usrList.size()>0){
					vo.setUsrNm(usrList.get(0).getRlNm());
				}
			}
			page.setResultList(voList);
		}
		return page;
	}
}
